#include "StdAfx.h"
#include "ReaderImpl.h"

#include "File.h"
#include "Header.h"
#include "HeaderReader.h"
#include "Point.h"


namespace las
{

	ReaderImpl::ReaderImpl(std::string const &strFileName)
	{
		m_pFile = new File;
		m_pFile->Open(strFileName, File::eRead);
		m_pHeaderReader = new HeaderReader(m_pFile);
		m_pHeader = NULL;
		m_pPoint = new Point;
	}


	ReaderImpl::~ReaderImpl(void)
	{
	}

	void ReaderImpl::ReadHeader()
	{
		m_pHeaderReader->ReadHeader();
		m_pHeader = m_pHeaderReader->GetHeader();

		m_pPoint->SetHeader(m_pHeader);

		Reset();
	}

	Header const& ReaderImpl::GetHeader() const
	{
		return *m_pHeader;
	}

	void ReaderImpl::Reset()
	{
		m_pFile->Seek(0, File::eBegin);

		m_CurrentPoint = 0;
		m_PointCount = m_pHeader->GetPointRecordsCount();
		m_PointSize = m_pHeader->GetPointRecordLength();
	}

	bool ReaderImpl::ReadNextPoint()
	{
		if (0 == m_CurrentPoint)
		{
			m_pFile->Seek(m_pHeader->GetDataOffset(), File::eBegin);
		}
		if (m_CurrentPoint >= m_PointCount )
		{
			return false;
		} 
		m_pFile->Read(m_pPoint->GetData(), m_PointSize);
		++m_CurrentPoint;

		return true;
	}

	Point const & ReaderImpl::GetPoint() const
	{
		return *m_pPoint;
	}

}

